<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCategories">添加分类</el-button>
        </el-col>
      </el-row>

      <tree-table
        :data="categoriesList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        border
        :show-row-hover="false"
      >
        <template slot-scope="scope" slot="isok">
          <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <template slot-scope="scope" slot="order">
          <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag type="warning" v-else="scope.row.cat_level===2">三级</el-tag>
        </template>
        <template slot-scope="scope" slot="opt">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="cateInfo.pagenum"
        :page-sizes="[5, 8, 10,15]"
        :page-size="cateInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <el-dialog
        title="添加分类"
        :visible.sync="addCategoriesVisible"
        width="50%"
        @close="closeCateDialog"
      >
        <el-form :model="cateForm" :rules="cateRules" ref="cateFormRef" label-width="100px">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="cateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              ref="cateRef"
              v-model="value"
              :options="cateParamsList"
              :props="cateProps"
              @change="handleChange"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCategoriesVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      cateInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      categoriesList: [],
      cateProps: {
        checkStrictly: true,
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
        children: "children"
      },
      total: 0,
      addCategoriesVisible: false,
      cateParamsList: [],
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          type: "template",
          template: "opt"
        }
      ],
      cateForm: { cat_name: "", cat_pid: 0, cat_level: 0 },
      cateRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      },
      value: []
    };
  },
  created() {
    this.getCategoriesList();
  },
  methods: {
    closeCateDialog() {
      this.value = [];
      this.cateForm.cat_pid = 0;
      this.cateForm.cat_level = 0;
      this.$refs.cateFormRef.resetFields();
      this.addCategoriesVisible = false;
    },
    async addCate() {
      this.$refs.cateFormRef.validate(async val=>{
        if(val){
          const { data: res } = await this.$http.post("categories", this.cateForm);
      if (res.meta.status != 201) {
        return this.$message.error("添加分类失败");
      } else {
        this.$message.success("添加分类成功");
        this.value = [];
        this.cateForm.cat_pid = 0;
        this.cateForm.cat_level = 0;
        this.$refs.cateFormRef.resetFields();
        this.addCategoriesVisible = false;
        this.getCategoriesList();
      }
        }
      })
    },
    handleChange() {
      if (this.value.length > 0) {
        this.cateForm.cat_pid = this.value[this.value.length - 1];
        this.cateForm.cat_level = this.value.length;
        return;
      } else {
        this.cateForm.cat_level = 0;
        this.cateForm.cat_pid = 0;
      }
    },
    async getCategoriesList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.cateInfo
      });
      if (res.meta.status != 200) {
        return this.$message.error("获取商品列表失败");
      } else {
        this.categoriesList = res.data.result;
        this.total = res.data.total;
      }
    },
    handleSizeChange(val) {
      this.cateInfo.pagesize = val;
      this.getCategoriesList();
    },
    handleCurrentChange(val) {
      this.cateInfo.pagenum = val;
      this.getCategoriesList();
    },
    addCategories() {
      this.getCateParamsList();
      this.addCategoriesVisible = true;
    },
    async getCateParamsList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      if (res.meta.status != 200) {
        return this.$message.error("获取父级参数列表失败");
      } else {
        this.cateParamsList = res.data;
      }
    }
  }
};
</script>
<style lang="less" scoped>
.el-row {
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
</style>